package com.tingyuge.base.thread.concurrent1;

/**
 * Created by xiaodao
 * date: 2019/11/26
 */
public class MyObjectWait {
    private int counter;

    public synchronized void add(){
        while(counter!=0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        counter++;
        System.out.println(counter);
        notify();
    }

    public synchronized void sub(){
        //这里不能是if 不然在多线程环境下会有问题.
        while (counter==0){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        counter--;
        System.out.println(counter);
        notify();
    }
}
